package com.example.administrator.ttphonehelper.contact;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.example.administrator.ttphonehelper.R;
import com.example.administrator.ttphonehelper.base.BaseHandlerActivity;
import com.example.administrator.ttphonehelper.bean.ContactBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

public class ContactActivity extends BaseHandlerActivity {
    @Bind(R.id.lv_contact)ListView mLvContact;
    @Bind(R.id.pb_contact_loading)ProgressBar mPbLoading;
    @Bind(R.id.et_search)EditText mEtSearch;

    //保存所有内容
    private List<ContactBean> mList;
    //保存临时搜索到的内容
    private List<ContactBean> mSearchList;
    private ContactAdapter mAdapter;
    private ContactManager mManager;

    //请求码
    public static final int REQUEST_CODE_UPDATE = 1;
    public static final int REQUEST_CODE_ADD = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        ButterKnife.bind(this);
        mManager = new ContactManager(this);
        mAdapter = new ContactAdapter(this);
        mList = new ArrayList<>();
        mSearchList = new ArrayList<>();
        getData();

        mLvContact.setAdapter(mAdapter);
    }
    //文本改变时间(查询联系人)
    @OnTextChanged(R.id.et_search) void search(Editable s){
        String search = s.toString();
        if (TextUtils.isEmpty(search)){
            mAdapter.setData(mList);
            mAdapter.notifyDataSetChanged();
            return;
        }
        mSearchList.clear();
        for (ContactBean bean :mList){
            //判断名字跟电话号码是否为空
            if (bean.getName()==null||bean.getMobileTel()==null){
                continue;
            }
            if(bean.getName().contains(search)||bean.getMobileTel().contains(search)){
                mSearchList.add(bean);
            }
        }
        mAdapter.setData(mSearchList);
        mAdapter.notifyDataSetChanged();
    }
    //添加点击事件
    @OnClick({R.id.bt_add,R.id.bt_quiry,R.id.bt_exit}) void click(View v){
        switch (v.getId()){
            case R.id.bt_add:
                Intent intent = new Intent(this,ContactUpdateActivity.class);
                startActivityForResult(intent,REQUEST_CODE_ADD);
                break;
            case R.id.bt_quiry:
                if (mEtSearch.getVisibility()==View.GONE){
                    mEtSearch.setVisibility(View.VISIBLE);
                }
                else {
                    mEtSearch.setVisibility(View.GONE);
                }
                break;
            case R.id.bt_exit:
                onBackPressed();
                break;
        }
    }
    //不同的返回码，区分不同的返回结果

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode!=-1){
            return;
        }
        switch (requestCode)
        {
            case REQUEST_CODE_UPDATE:
            case REQUEST_CODE_ADD:
                getData();
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //获取数据
    public void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //加载数据
                mList = mManager.queryAll();
                mAdapter.setData(mList);
                //通知UI线程更新界面
                mHandler.sendEmptyMessage(1);
            }
        }).start();
    }

    @Override
    public void onHandler(Message msg) {
        switch (msg.what){
            case 1:
                mPbLoading.setVisibility(View.GONE);
                mAdapter.notifyDataSetChanged();
                break;
        }
    }
}
